package com.dz.lm.web.controller.newuser.usermessage;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.usermessage.UserMessage;
import com.dz.lm.service.usermessage.UserMessageService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/6/30 16:59;
 */
@Controller
@RequestMapping(value = "/h5/weijie/rewriteUrl/userMessage", produces = "application/json")
public class UserMessageController extends BasicControl {
    @Autowired
    private UserMessageService userMessageService;
    /**
     * 添加私信
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //登录人
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //发送对方用户
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //私信内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        UserMessage userMessage = new UserMessage();
        userMessage.setUserId(userId);
        userMessage.setSendUid(StringUtil.getInt(uId,0));
        userMessage.setMessage(content);
        userMessage.setCreateTime(DateUtil.getTimeStr(new Date()));
        int add = userMessageService.add(userMessage);
        if(add > 0){
            result ="00";
            message="发送成功";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 按人删除私信
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/deleteByUser")
    public Map deleteByUser(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //登录人
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //主键
        Integer recordId = param.get("recordId") != null ? Integer.parseInt(param.get("recordId").toString()) : 0;
        int i = userMessageService.deleteByUser(recordId);
        if(i > 0){
            result ="00";
            message="操作成功";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 查看登录人消息列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/query")
    public Map query(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //登录人
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = userMessageService.query(pageNum+1,rows,StringUtil.getInt(uId,0));
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 查看消息详情
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/getDetail")
    public Map getDetail(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //登录人
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        //联系人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        data = userMessageService.getDetail(pageNum+1,rows,StringUtil.getInt(uId,0),userId);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 消息数量
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/getCount")
    public Map getCount(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //登录人
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        int count = userMessageService.getCount(StringUtil.getInt(uId, 0));
        Map map = new HashMap();
        map.put("count",count);
        data= map;
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
}
